//
//  MovieManager.m
//  EasyToo
//
//  Created by zhengzhe on 13-8-24.
//
//

#import "MovieManager.h"
#import "ServerCommunicationManager.h"

#import "QryMovieDetailRequest.h"
#import "QryMovieDetailResponse.h"

#import "QryOnlineTicketRequest.h"
#import "QryOnlineTicketResponse.h"

#import "QryCinemaDetailRequest.h"
#import "QryCinemaDetailResponse.h"

#import "QryOnlineMoviesRequest.h"
#import "QryOnlineMoviesResponse.h"

#import "BuyOnlineTicketRequest.h"
#import "BuyOnlineTicketResponse.h"

#import "BuyCommonTicketRequest.h"
#import "BuyCommonTicketResponse.h"

#import "ConfirmOnlineTicketRequest.h"
#import "ConfirmOnlineTicketResponse.h"

#import "QryCommonTicketRequest.h"
#import "QryCommonTicketResponse.h"


@implementation MovieManager

static MovieManager* movieManager = nil;

+ (MovieManager *)shareManager
{
    if (movieManager == nil) {
        movieManager = [[MovieManager alloc] init];
    }
    return movieManager;
}



//查询当日上映电影
- (void)qryOnlineMovie
{
    QryOnlineTicketRequest *qryOnlineMovieRequest = [[QryOnlineTicketRequest alloc]initQryOnlieMovie:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:qryOnlineMovieRequest];
}

//查询电影详情
- (void)qryMovieDetail:(NSString*)filmId
{
    QryMovieDetailRequest *qryMovieDetailRequest = [[QryMovieDetailRequest alloc]initQryMovieDetail:self filmId:filmId];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:qryMovieDetailRequest];
}

//查看影院
- (void)qryCinemaDetail:(NSString*)filmId cinemaId:(NSString*)cinemaId
{
    QryCinemaDetailRequest *qryCinemaRequest = [[QryCinemaDetailRequest alloc]initQryCinemaDetail:self
                                                                                           filmId:filmId
                                                                                         cinemaId:cinemaId];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:qryCinemaRequest];
}

//查看在线座位
- (void)qryOnlineDetail:(NSString*)foretellId hallId:(NSString*)hallId cinemaId:(NSString*)cinemaId
{
    QryOnlineMoviesRequest *qryOnlineRequest = [[QryOnlineMoviesRequest alloc]initQryOnlineMovies:self
                                                                                       foretellId:foretellId
                                                                                           hallId:hallId
                                                                                         cinemaId:cinemaId];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:qryOnlineRequest];
}

//购买在线票
- (void)buyOnlineTicket:(NSString*)foretellId seatIds:(NSString*)seatIds
                   fees:(NSString*)fees
                 prices:(NSString*)prices
                  count:(NSString*)count
{
    BuyOnlineTicketRequest *qryOnlineRequest = [[BuyOnlineTicketRequest alloc]buyOnlineTickets:self
                                                                                    foretellId:foretellId
                                                                                       seatIds:seatIds
                                                                                          fees:fees
                                                                                        prices:prices
                                                                                         count:count];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:qryOnlineRequest];
}

//购买通票
- (void)buyCommonTicket:cinemaName
               cinemaId:cinemaId
                  count:count
              userPhone:userPhone
                  price:price
                    tid:tid
{
    BuyCommonTicketRequest *buyCommonRequest = [[BuyCommonTicketRequest alloc]buyCommonTickets:self
                                                                                    cinemaName:cinemaName
                                                                                      cinemaId:cinemaId
                                                                                         count:count
                                                                                     userPhone:userPhone
                                                                                         price:price
                                                                                           tid:tid];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:buyCommonRequest];

}

//确认购买
- (void)confirmonlineTickets:(NSString*)orderId
                  cinemaName:(NSString*)cinemaName
                      prices:(NSString*)prices
                    showtime:(NSString*)showtime
                    showdate:(NSString*)showdate
                  foretellId:(NSString*)foretellId
                          rk:(NSString*)rk
                        fees:(NSString*)fees
                       count:(NSString*)count
                    allprice:(int)allprice
{
    ConfirmOnlineTicketRequest *buyCommonRequest = [[ConfirmOnlineTicketRequest alloc]confirmonlineTickets:self
                                                                                                    orderId:orderId
                                                                                                 cinemaName:cinemaName
                                                                                                     prices:prices
                                                                                                   showtime:showtime
                                                                                                   showdate:showdate
                                                                                                 foretellId:foretellId
                                                                                                         rk:rk
                                                                                                       fees:fees
                                                                                                      count:count
                                                                                                  allprice:allprice];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:buyCommonRequest];
}

//查看通票
- (void)qryCommonTicketInfo
{
    QryCommonTicketRequest *request = [[QryCommonTicketRequest alloc] qryCommonTicket:self];
    [[ServerCommunicationManager GetServerCommunication] sendRequest:request];
}




//票务处理成功
- (void)onRequestSuccess:(BaseResponse *)response
{
    if ([response.request isKindOfClass:[QryOnlineTicketRequest class]])
    {
        [self handleQryOnlineTicketSuccess:(QryOnlineTicketResponse*)response];
    }
    else if ([response.request isKindOfClass:[QryMovieDetailRequest class]])
    {
        [self handleQryDetailMoviesSuccess:(QryMovieDetailResponse*)response];
    }
    else if ([response.request isKindOfClass:[QryCinemaDetailRequest class]])
    {
        [self handleQryCinemaSuccess:(QryCinemaDetailResponse*)response];
    }
    else if ([response.request isKindOfClass:[QryOnlineMoviesRequest class]])
    {
        [self handleQryOnlineMoviesSuccess:(QryOnlineMoviesResponse*)response];
    }
    else if ([response.request isKindOfClass:[BuyOnlineTicketRequest class]])
    {
        [self handleBuyOnlineTicketSuccess:(BuyOnlineTicketResponse*)response];
    }
    else if ([response.request isKindOfClass:[BuyCommonTicketRequest class]])
    {
        [self buyCommonTicketSuccess:(BuyCommonTicketResponse*)response];
    }
    else if ([response.request isKindOfClass:[ConfirmOnlineTicketRequest class]])
    {
        [self confirmOnlineTicketSuccess:(ConfirmOnlineTicketResponse*)response];
    }
    else if ([response.request isKindOfClass:[QryCommonTicketRequest class]])
    {
        [self qryCommonTicketSuccess:(QryCommonTicketResponse*)response];
    }
    
} 

//票务处理失败
- (void)onRequestFail:(BaseResponse *)response
{
    if ([response.request isKindOfClass:[QryOnlineTicketResponse class]])
    {
        [self.delegate qryOnlineMoviesFinish:NO Movies:nil];
    }
    else if ([response.request isKindOfClass:[QryMovieDetailRequest class]])
    {
        [self.delegate qryDetailMoviesFinish:NO Movies:nil];
    }
    else if ([response.request isKindOfClass:[QryCinemaDetailRequest class]])
    {
        CinemaInfo *cinema = [[[CinemaInfo alloc] init]autorelease];
        [self.delegate qryCinemaFinish:NO Cinema:cinema];
    }
    else if ([response.request isKindOfClass:[QryOnlineMoviesRequest class]])
    {
        [self.delegate qryOnlieSeatsFinish:NO CinemaSeatInfo:nil];
    }
    else if ([response.request isKindOfClass:[BuyOnlineTicketRequest class]])
    {
        [self.delegate buyOnlineTicket:NO orderId:nil rk:nil];
    }
    else if ([response.request isKindOfClass:[BuyCommonTicketRequest class]])
    {
        [self.delegate buyCommonTicket:NO tn:nil];
    }
    else if ([response.request isKindOfClass:[ConfirmOnlineTicketRequest class]])
    {
        [self.delegate confirmOnlineTicket:NO tn:nil];
    }
    else if ([response.request isKindOfClass:[QryCommonTicketRequest class]])
    {
        [self.delegate qryCommontTickets:NO commonTicket:nil];
    }
}





//查询当日上映电影
- (void)handleQryOnlineTicketSuccess:(QryOnlineTicketResponse*)response
{
    NSMutableArray *movieInfoArray = response.movieInfoArray;
    if (movieInfoArray)
    {
        [self.delegate qryOnlineMoviesFinish:YES Movies:movieInfoArray];
    }
    else
    {
        [self.delegate qryOnlineMoviesFinish:NO Movies:nil];
    }
}


//查看电影详情
- (void)handleQryDetailMoviesSuccess:(QryMovieDetailResponse*)response
{
    NSMutableArray *movieInfoArray = response.movieDetailArray;
    if (movieInfoArray)
    {
        [self.delegate qryDetailMoviesFinish:YES Movies:movieInfoArray];
    }
    else
    {
        [self.delegate qryDetailMoviesFinish:NO Movies:nil];
    }
}

//查看影院
- (void)handleQryCinemaSuccess:(QryCinemaDetailResponse*)response
{
    CinemaInfo *cinemaInfo = response.cinemaInfo;
    if (cinemaInfo)
    {
        [self.delegate qryCinemaFinish:YES Cinema:cinemaInfo];
    }
    else
    {
        [self.delegate qryCinemaFinish:NO Cinema:nil];
    }
}

//查看在线座位
- (void)handleQryOnlineMoviesSuccess:(QryOnlineMoviesResponse*)response
{
    CinemaSeatInfo *cinemaArray = response.cinemaArray;
    if (cinemaArray)
    {
        [self.delegate qryOnlieSeatsFinish:YES CinemaSeatInfo:cinemaArray];
    }
    else
    {
        [self.delegate qryOnlieSeatsFinish:NO CinemaSeatInfo:nil];
    }
}

//购买在线座位
- (void)handleBuyOnlineTicketSuccess:(BuyOnlineTicketResponse*)response
{
    NSString *orderId = response.orderId;
    NSString *rk = response.rk;
    if (orderId && rk)
    {
        [self.delegate buyOnlineTicket:YES orderId:orderId rk:rk];
    }
    else
    {
        [self.delegate buyOnlineTicket:NO orderId:orderId rk:rk];
    } 
}

//确认通票
- (void)buyCommonTicketSuccess:(BuyCommonTicketResponse*)response
{
    NSString *tn = response.tn;
    if (tn)
    {
        [self.delegate buyCommonTicket:YES tn:tn];
    }
    else
    {
        [self.delegate buyCommonTicket:NO tn:tn];
    }
}

//确认购买
- (void)confirmOnlineTicketSuccess:(ConfirmOnlineTicketResponse*)response
{
    NSString *tn = response.tn;
    if (tn)
    {
        [self.delegate confirmOnlineTicket:YES tn:tn];
    }
    else
    {
        [self.delegate confirmOnlineTicket:NO tn:tn];
    }
}

//查看通票
- (void)qryCommonTicketSuccess:(QryCommonTicketResponse*)response
{
    CommonTicketInfo *cinemaArray = response.cinemaInfo;
    if (cinemaArray)
    {
        [self.delegate qryCommontTickets:YES commonTicket:cinemaArray];
    }
    else
    {
        [self.delegate qryCommontTickets:NO commonTicket:nil];
    }
}

@end
